<?php

class Ctrl_berita extends CI_Controller {

	function __construct() {
		parent::__construct();
		if (!$this->session->userdata('adminin')) {
			redirect(base_url() . 'admin/home');
		}
		$this->load->model('Model_berita');
	}

	function index() {
		//menampilkan tabel yang berisi berita yang telah ada dan pilihan untuk delete dan edit
		$this->tampil_daftar();
	}

	function tampil_daftar() {
		$query = $this->Model_berita->select_all();
		if ($query->num_rows() > 0) {
			$this->load->library('table');
			$this->table->set_template(array('table_open' => '<table id="list_berita" align="center" width="600" border="0" cellpadding="4" cellspacing="3">'));
			$this->table->set_heading('No', 'Judul', 'Status', 'Aksi');
			$i = 1;
			foreach ($query->result() as $entry) {
				$this->table->add_row(
						$i, substr($entry->judul, 0, 30) . '...', $entry->status, '<span class="anchor"><a href="' . base_url() . 'admin/ctrl_berita/view/' . $entry->idberita . '">Lihat</a></span>' .
						' | ' .
						'<span class="anchor"><a href="' . base_url() . 'admin/ctrl_berita/edit/' . $entry->idberita . '">Edit</a></span>' .
						' | ' .
						'<span class="anchor"><a onclick="return confirm_message(\'Apakah anda yakin akan menghapus?\')" href="' . base_url() . 'admin/ctrl_berita/delete/' . $entry->idberita . '">Hapus</a></span>'
				);
				$i++;
			}
			$display['content'] = $this->table->generate();
		} else {
			$display['content'] = '<center>Belum ada berita yang dibuat</center>';
		}
		$data['title'] = 'Daftar Berita';
		$this->load->view('admin/admin_header_view', $data);
		$this->load->view('admin/list_berita', $display);
		$this->load->view('admin/admin_footer_view');
	}

	function buat_berita() {
		$this->load->library('wysiwyg');
		$data['script'] = $this->wysiwyg->loadscript();
		$data['title'] = 'Buat Berita';
		$this->load->view('admin/admin_header_view');
		$this->load->view('admin/form_berita');
		$this->load->view('admin/admin_footer_view');
	}

	function insert() {
		$config['upload_path'] = './gbr_artikel/';
		$config['file_name'] = $this->Model_berita->count();
		$config['allowed_types'] = 'gif|jpg|png';
		$this->load->library('upload', $config);

		if (!$this->upload->do_upload('gambar')) {
			
		} else {
			$gbr = $this->upload->data();
			$this->Model_berita->gambar = base_url() . 'gbr_artikel/' . $config['file_name'] . $gbr['file_ext'];
		}

		if ($this->input->post('judul') != '') {
			$this->Model_berita->judul = $_POST['judul'];
			$this->Model_berita->pembuat = $_POST['pembuat'];
			//$this->Model_berita->preview=$_POST['preview'];
			$this->Model_berita->more = $_POST['more'];
			$this->Model_berita->status = $_POST['status'];
			$this->Model_berita->insert();
			redirect(base_url() . 'admin/ctrl_berita');
		} else {
			$data['pesan'] = "Harap isi judul berita <a href='javascript:back()'>Kembali</a>";
			$this->load->view('admin/admin_header_view');
			$this->load->view('admin/hal_pesan', $data);
			$this->load->view('admin/admin_footer_view');
		}
	}

	function view($id) {
		$query = $this->Model_berita->select(array("idberita" => $id));
		$entry = $query->row();
		$display['content']['judul'] = $entry->judul;
		$display['content']['gambar'] = $entry->gambar;
		$display['content']['pembuat'] = $entry->pembuat;
		$display['content']['tgl'] = $entry->tglbuat;
		$display['content']['isi'] = $entry->more;

		$data['title'] = "Lihat Berita";
		$this->load->view('admin/admin_header_view', $data);
		$this->load->view('admin/lihat_berita', $display);
		$this->load->view('admin/admin_footer_view');
	}

	function edit($id) {
		$query = $this->Model_berita->select(array("idberita" => $id));
		$entry = $query->row();
		$display['content']['id'] = $entry->idberita;
		$display['content']['judul'] = $entry->judul;
		$display['content']['pembuat'] = $entry->pembuat;
		$display['content']['more'] = $entry->more;
		$display['content']['status'] = $entry->status;
		$this->load->view('admin/admin_header_view');
		$this->load->view('admin/edit_berita', $display);
		$this->load->view('admin/admin_footer_view');
	}

	function update() {
		$id = $_POST['id'];
		$data = array(
			'judul' => $_POST['judul'],
			'pembuat' => $_POST['pembuat'],
			'more' => $_POST['more'],
			'status' => $_POST['status'],
		);
		$this->Model_berita->update($id, $data);
		redirect(base_url() . 'admin/ctrl_berita');
	}

	function delete($id) {
		$this->Model_berita->delete($id);
		redirect(base_url() . 'admin/ctrl_berita');
	}
}